package com.softWang.system.po;

import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.softWang.common.base.ExcelEnumCovertInterface;
import com.softWang.common.base.TenantBasePO;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户实体
 */
@TableName(value = "system_users", autoResultMap = true)
@KeySequence("system_users_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库，可不写。
@Data
@EqualsAndHashCode(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SystemUsers extends TenantBasePO {

    @ApiModelProperty(value = "主键id")
    @TableId
    private Long id;

    @ApiModelProperty(value = "用户名，最大长度为60位")
    @NotBlank(message = "uname不能为空")
    @Size(max = 60, message = "uname最大长度为60位")
    private String uname;


    @ApiModelProperty(value = "登录账户，最大长度为60位")
    @NotBlank(message = "loginName不能为空")
    @Size(max = 60,message = "loginName最大长度为60位")
    private String loginName;

    @ApiModelProperty(value = "类型，(1.超级管理员，2.创建的普通管理员,3.租户管理员)")
    private Integer type;

    @ApiModelProperty(value = "所属部门id")
    private Long deptid;

    @ApiModelProperty(value = "状态，0:正常,1:停用")
    private Integer state;

    /**
     * MD5(password+loginname)
     */
    private String password;

    @ApiModelProperty(value = "手机号号码")
    private String tel;

    @ApiModelProperty(value = "性别，1:男;2:女;0:未知")
    private String sex;

    @ApiModelProperty(value = "邮箱")
    private String email;

    @ApiModelProperty(value = "用户头像")
    private Long avatar;

    @ApiModelProperty(value = "密码过期时间")
    private LocalDateTime passValidDate;

    @TableField(exist = false)
    private List<Long> roleIds;

    /**
     * 用户状态枚举
     */
    public enum UserState {
        /**
         * 正常
         */
        NORMAL(0),
        /**
         * 停用
         */
        DISABLE(1);


        private Integer code;

        UserState(Integer code) {
            this.code = code;
        }
        public Integer getCode() {
            return this.code;
        }

    }

    /**
     * 用户类型枚举
     */
    public enum UserType implements ExcelEnumCovertInterface {

        SUPPERADMIN(1,"超级管理员"),
        ADMIN(2,"管理员"),
        TENANT_ADMIN(3,"租户管理员");


        private Integer code;

        private String msg;

        UserType(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }
        public Integer getCode() {
            return this.code;
        }
        public String getMsg() {
            return this.msg;
        }

        /**
         * 根据code获取枚举
         * @param code 状态码
         * @return 对应的枚举，找不到返回null
         */
        public static UserType getByCode(Integer code) {
            if (code == null) {
                return null;
            }
            for (UserType state : values()) {
                if (state.getCode().equals(code)) {
                    return state;
                }
            }
            return null;
        }

        /**
         * 根据msg获取枚举
         * @param msg 状态描述
         * @return 对应的枚举，找不到返回null
         */
        public static UserType getByMsg(String msg) {
            if (msg == null) {
                return null;
            }
            for (UserType state : values()) {
                if (state.getMsg().equals(msg)) {
                    return state;
                }
            }
            return null;
        }

        public String getExcelLable(Integer value) {
            UserType commonState = getByCode(value);
            if (commonState == null) {
                return null;
            }
            return commonState.getMsg();
        }


        public Integer getJavaValue(String label) {
            UserType commonState = getByMsg(label);
            if (commonState == null) {
                return null;
            }
            return commonState.getCode();
        }
    }

}